package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.system.SysCityInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysCityInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 全国城市信息Controller
 *
 * @author zjw
 * @date 2023-10-07
 */
@RestController
@RequestMapping("/system/cityInfo")
public class SysCityInfoController extends BaseController {
    @Autowired
    private ISysCityInfoService sysCityInfoService;

    /**
     * 查询全国城市信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCityInfo sysCityInfo) {
        startPage();
        List<SysCityInfo> list = sysCityInfoService.selectSysCityInfoList(sysCityInfo);
        return getDataTable(list);
    }

    /**
     * 导出全国城市信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "全国城市信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCityInfo sysCityInfo) {
        List<SysCityInfo> list = sysCityInfoService.selectSysCityInfoList(sysCityInfo);
        ExcelUtil<SysCityInfo> util = new ExcelUtil<SysCityInfo>(SysCityInfo.class);
        util.exportExcel(response, list, "全国城市信息数据");
    }

    /**
     * 获取全国城市信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysCityInfoService.selectSysCityInfoById(id));
    }

    /**
     * 根据城市名称查询对应城市信息。
     *
     * @param cityName
     * @return
     */
    @GetMapping(value = "/getCityInfoByName/{cityName}")
    public AjaxResult getCityInfoByName(@PathVariable("cityName") String cityName) {
        return success(sysCityInfoService.getCityInfoByCityName(cityName));
    }

    /**
     * 新增全国城市信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "全国城市信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCityInfo sysCityInfo) {
        return toAjax(sysCityInfoService.insertSysCityInfo(sysCityInfo));
    }

    /**
     * 修改全国城市信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "全国城市信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCityInfo sysCityInfo) {
        return toAjax(sysCityInfoService.updateSysCityInfo(sysCityInfo));
    }

    /**
     * 删除全国城市信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "全国城市信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysCityInfoService.deleteSysCityInfoByIds(ids));
    }
}
